<?php 
	session_start();
	require("connect.php");
?>
<html><!-- InstanceBegin template="/Templates/temp02.dwt.php" codeOutsideHTMLIsLocked="false" -->
<style type="text/css">
<!--
#subcontent {
	position:absolute;
	left:243px;
	top:0px;
	width:706px;
	height:59px;
	z-index:1;
	background-color: #70a972;
}
#subcontent01 {
	position:absolute;
	left:22px;
	top:11px;
	width:287px;
	height:39px;
	font:Tahoma;
	font-size:30px;
	font-weight:600px;
	color:#E8E8E8;
	z-index:1;
}
#subcontent02 {
	position:absolute;
	left:312px;
	top:29px;
	font:Tahoma;
	font-size:18px;
	font-weight:500px;
	color:#E8E8E8;
	width:199px;
	height:25px;
	z-index:2;
}
-->
</style>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- InstanceBeginEditable name="doctitle" -->
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="css/product.css" />

<!-- InstanceEndEditable -->
<!-- InstanceBeginEditable name="head" --><!-- InstanceEndEditable -->
</head>

<body>
<?php 
function GetQuantity($id){
		$cart = $_SESSION["CART"];
		$quantity=0;
		foreach(array_keys($cart) as $value){
			if($value == $id){
				$quantity = $cart[$id];
				break;
			}
		}
		return $quantity;	
	} 
?>
<div id="wrap">
	<div id="title">
		<div id="title01">
			<span><a href="register.php">Register </a>|<a href="mycart.php"> My Cart </a>|<a href="login.php"> Login </a></span>
		</div>
	</div>
	<div id="header">
		<div id="searchform">
	    	<form action="Templates/search.php">
					<input type="text" name="s" value="" onKeyPress="this.style.backgroundColor='pink'" onBlur="this.style.backgroundColor='white'" onFocus="this.style.backgroundColor='pink'"/>
			</form>
		</div>
	</div>
	<div id="menu">
		<ul>
			<li><a href="index.php">TRANG CHỦ</a></li>
			<li><a href="introduce.php">GIỚI THIỆU</a></li>
			<li><a href="product_view.php">SẢN PHẨM</a></li>
			<li><a href="promotion.php">KHUYẾN MẠI</a></li>			
			<li><a href="tuvan.php">TƯ VẤN THIẾT BỊ</a></li>
			<li><a href="phanhoi.php">PHẢN HỒI</a></li>
		</ul>
   </div>
   <div id="submenu">
   		<div id="submennu01"><a href="#">Uy tín/Chất lượng</a></div>
  </div>
   <div id="center">
   		<div id="left">
			<div id="danhmuc"><a href="product_view.php?catid=0">DANH MỤC SẢN PHẨM</a></div>
			<div id="left01">
			<?php 
						$sql=mysql_query("select * from catagory");
						while($row=mysql_fetch_array($sql))
					{
					    echo "<font color=#F8F8F8>::</font>";
						echo "<a href='product_view.php?catid={$row["catID"]}'>{$row["catName"]}</a><br>";
						echo "<font color=#666633 >-------------------------------</font><br>";
					}
			?>
			</div>
			<div id="giohang">
				<div id="giohang01"><b style="font:Tahoma; font-size:14px; color:#FFFFFF;">GIỎ HÀNG CỦA BẠN</b></div>
			</div>
			<div id="cart">
			  <?php include("cartdetail.php");?></div>
		</div>
		<div id="subcontent">
			<div id="subcontent01">NỘI THẤT CAO CẤP</div>
			<div id="subcontent02">UY TÍN - CHẤT LƯỢNG</div>
		</div>
		<div id="content"><!-- InstanceBeginEditable name="vungsuaduoc" -->
		<table width="98%" height="798" border="0" align="center" cellpadding="0" cellspacing="0">
              <tr>
                <td valign="top"><?php 
	$cart = $_SESSION["CART"];
	$action = $_REQUEST["action"];
	switch($action){
		case "add":
				$id = $_REQUEST["pid"];	
				if($cart){
					if(array_key_exists($id,$cart))
					{
						$cart[$id] +=1;
					}else{
						$cart[$id]=1;
					}
				}else{
					$cart[$id]=1;
				}
				$_SESSION["CART"] = $cart;
				echo"<script language=javascript>document.location='cart.php'</script>";
			break;
		case "update":
		
			foreach(array_keys($cart) as $value){
				if($_REQUEST["quantity_".$value] >0)
				$cart[$value] = $_REQUEST["quantity_".$value];
			}
			$_SESSION["CART"] = $cart;
			
			break;
		case "delete":
			$id = $_REQUEST["pid"];
			if($cart)
			{
				foreach(array_keys($cart) as $value){
					if($value != $id){
						$newcart[$value] = $cart[$value];
					}
				}
				$_SESSION["CART"] = $newcart;	
				$cart=$newcart;
			}
			echo"<script language=javascript>document.location='cart.php'</script>";
			break;
	
	}
	
	

?>
<style type="text/css">
<!--
.style3a {font-family: tahoma; font-size: 14px; }
.style9a {font-size: 14}
.style10a {
	font-family: tahoma;
	font-weight: bold;
	font-size: 14px;
}
.style11a {font-size: 14px}
.style14a {font-family: tahoma}
.style16a {font-family: tahoma; font-weight: bold;font-size:12px }
-->
</style>
<script>
	a = true;
	function IsNumeric(txt)
	{
		   sText = txt.value;
		   var ValidChars = "0123456789";
		   var IsNumber=true;
		   var Char;
		
		 
		   for (i = 0; i < sText.length && IsNumber == true; i++) 
			  { 
			  Char = sText.charAt(i); 
			  if (ValidChars.indexOf(Char) == -1) 
				 {
				 IsNumber = false;
				 a = false;
				 alert('Số lượng sản phẩm phải là số !');
				 document.location="cart.php";
				 }
			  }
		   return IsNumber;
   
     }
   	 function test(frm)
   	 {
		if(a){
			document.location="checkout.php";}
     }
</script>
<form id="form1" name="form1" method="post" action="cart.php" onSubmit="return a;">
<input type="hidden" id="action" name="action" value="update" />
	<table width=531 align=center>
	<tr bgcolor=#006633><td width=20% height=15 bgcolor="#FF6600"></td>
	<td width=21% height=15 bgcolor="#FF6600"></td>
	<td width=21% height=15 bgcolor="#FF6600"></td>
	<td width=20% height=15 bgcolor="#FF6600"></td>
	<td width=18% height=15 bgcolor="#FF6600"></td>
	</table>
    <table width="89%" border="0" align="center" cellpadding="0" cellspacing="0" >
      <tr>
        <td colspan=5><fieldset >
          <center>
          </center>
        </fieldset></td>
      </tr>
      <tr>
        <td width="35%"><div align="left" class="style4">
            <div align="left"><strong><span class="style41">Tên Sản Phẩm</span></strong></div>
        </div></td>
        <td width="25%"><strong><span class="style41">Số Lượng</span></strong></td>
        <td width="10%"><div align="center"><strong><span class="style41">&nbsp;&nbsp;&nbsp;Giá</span></strong></div></td>
        <td width="10%"><div align="center"><span class="style43">Tiền</span></div></td>
        <td width="10%"><div align="center"><strong><span class="style41">Bỏ chọn</span></strong></div></td>
      </tr>
      <tr>
        <td><div align="left"></div></td>
        <td><div align="left"></div></td>
        <td><div align="left"></div></td>
        <td><div align="left"></div></td>
        <td><div align="left"></div></td>
      </tr>
      <?php 
		if($cart){
		$idlist = "";
		$total=0;
		foreach(array_keys($cart) as $value){
			$idlist = $idlist.$value.",";
		}
		$idlist = $idlist."0";
		$sql1 = "SELECT * FROM product WHERE pID IN (".$idlist.")";
		
		require_once("connect.php");
		
		$result = mysql_query($sql1) or die(mysql_error());
		while($row = mysql_fetch_array($result)){
	?>
      <tr>
        <td><div align="left"><span class="style9a"><?php  echo $row["pName"]?></span></div></td>
        <td><div align="center"><span class="style9a"> </span>
                <label> <span class="style9a"> </span> <span class="style9a">
                <input name="quantity_<?php  echo $row["masanpham"]?>" type="text" id="quantity_<?php echo $row["pID"]?>" value="<?php  echo GetQuantity($row["pID"])?>" style="width:50px" onBlur="return IsNumeric(this)"/>
                </span> </label>
        </div></td>
        <td><div align="left">&nbsp;&nbsp;&nbsp;&nbsp;<span class="style9a"><?php  echo $row["pPrice"]?></span></div></td>
        <td><div align="left"><span class="style11a">
            <?php  $total += $row["pPrice"]*GetQuantity($row["pID"]);echo $row["pPrice"]*GetQuantity($row["pID"]);?>
          VNĐ . </span></div></td>
        <td><div align="center"><span class="style9a"><a href="cart.php?action=delete&pid=<?php echo $row["pID"]?>"><font color=red>Bỏ chọn</font></a></span></div></td>
      </tr>
      <?php 
		}
		
	}else{
	?>
      <tr>
        <td><div align="center" class="style44">
            <div align="right"><span class="style2">Không có hàng trong giỏ</span>!</div>
        </div></td>
        <td><div align="left"><span class="style9a"></span></div></td>
        <td><div align="left"><span class="style9a"></span></div></td>
        <td><div align="left"><span class="style11a"></span></div></td>
        <td><div align="left"><span class="style9a"></span></div></td>
      </tr>
      <?php 	
		}
	?>
      <tr>
        <td><div align="left"></div></td>
        <td><div align="left"></div></td>
        <td><div align="left"></div></td>
        <td><div align="left"></div></td>
        <td><div align="left"></div></td>
      </tr>
      <tr>
        <td><div align="left"><span class="style9a"></span></div></td>
        <td><div align="left"><span class="style9a"></span></div></td>
        <td><div align="center" class="style11a">
            <div align="right"><span class="style16a">Tộng Cộng : </span></div>
        </div></td>
        <td colspan="2"><div align="center" class="style11a">
            <div align="left"><span class="style14a"><strong><?php  echo $total?>
                    <?php  if ($_SESSION["disableadd"]==1)
					{?>
              VNĐ .
              <?php  ;}else {?>
              0 VNĐ .
              <?php  }?>
            </strong> </span></div>
        </div>
            <div align="left"><span class="style9a"> </span></div>
          <span class="style9a">
            <label></label>
          </span></td>
      </tr>
      <tr>
        <td><div align="left"></div></td>
        <td><div align="left"></div></td>
        <td><div align="left"></div></td>
        <td><div align="left"></div></td>
        <td><div align="left"></div></td>
      </tr>
      <tr>
        <td><div align="left"></div></td>
        <td><div align="left"></div></td>
        <?php  if ($_SESSION["disableadd"]==1){?>
        <td><div align="left"><span class="style9a">
            <input name="Submit" type="submit" class="style7" value="Cập Nhật Số Lượng" />
        </span></div></td>
        <?php  ;}else {?>
        <td><div align="left"><span class="style9a">
            <input name="Submit" type="submit" disabled class="style7" value="Cập Nhật Số Lượng">
            <?php }?>
        </span></div></td>
        <td><div align="left">
            <?php  if ($_SESSION["laptop"]=="true") {?>
            <label>
            <input name="Submit2" type="button" class="style7" onClick="window.location = 'product_view.php';" value="Chọn tiếp"/>
            </label>
            <?php  ;} else {?>
            <label>
            <input name="Submit2" type="button" class="style7" onClick="window.location = 'product_view.php';" value="Chọn tiếp"/>
            </label>
            <?php  }?>
        </div></td>
        <?php  if ($_SESSION["disableadd"]==1){?>
        <td><div align="left">
            <?php  if ($total<=100000000){?>
            <label>
            <input name="Submit3" type="button" class="style7" onClick="test(this.form)" value="Thanh Toán"/>
            </label>
            <?php  ;}else {?>
            <label>
            <input name="Submit3" type="button" class="style7" onClick="alert('Số tiền bạn sẽ phải trả cho số sản phẩm chọn mua lớn hơn 100.000.000 đ .\n liên hệ tới công ty để có thể giao dịch ngay tức thì !')" value="Thanh Toán"/>
            </label>
            <?php  ;}?>
        </div></td>
        <?php  ;}else {?>
        <td><div align="left">
            <label>
            <input name="Submit3" type="button" disabled class="style7" onClick="test(this.form)" value="Thanh Toán">
            </label>
        </div></td>
        <?php }?>
      </tr>
    </table>
</form> </td>
              </tr>
            </table>
		<!-- InstanceEndEditable --></div>
   </div>
   <div id="footer01">
  		<center><a href="#">Design By H3TL&nbsp;Group3<br> Mọi thông tin chi tiết xin liên hệ: h3tl.itpro@gmail.com</a></center>
  </div>
   
</div>


</body>
<!-- InstanceEnd --></html>
